﻿using AutoMapper;
using IMES.Common.Domain.DurianModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;
using System.Collections.Generic;

namespace Durian.API.Application.Commands.Process
{
    public class ProcessLogSelectHandler : IRequestHandler<ProcessLogSelectCommand, Result>
    {
        //private readonly IMapper mapper;
        private readonly ILogger<ProcessManagementCreateHandler> logger;
        private readonly IBaseRepositorys<SystemLogs> systemLogsRepository;

        public ProcessLogSelectHandler(IBaseRepositorys<SystemLogs> systemLogsRepository, ILogger<ProcessManagementCreateHandler> logger)
        {
            this.systemLogsRepository = systemLogsRepository;
            this.logger = logger;
        }

        /// <summary>
        /// 日志显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(ProcessLogSelectCommand request, CancellationToken cancellationToken)
        {
            //LogeTime  日志时间
            try
            {
                var res = new Result() { Code = (int)CodeEnum.search };
                var process = systemLogsRepository.GetAll();
                //操作时间(开始时间)
                if (!string.IsNullOrEmpty(request.start))
                {
                    process = process.Where(x => x.LogeTime >= DateTime.Parse(request.start));
                }
                //操作时间(结束时间)
                if (!string.IsNullOrEmpty(request.end))
                {
                    process = process.Where(x => x.LogeTime <= DateTime.Parse(request.end));
                }
                res.Code = (int)CodeEnum.search;
                res.Messege = "工序管理日志显示成功";
                logger.LogInformation("工序管理日志显示成功");
                res.Data = process;
                return res;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "工序管理日志显示失败");
                return new Result() { Code = (int)CodeEnum.error, Messege = "工序管理日志显示失败" };
            }
        }
    }
}
